//
//  UIImage+My.m
//  SinaWebo
//
//  Created by 亦心 on 15/3/8.
//  Copyright (c) 2015年 亦心. All rights reserved.
//

#import "UIImage+Extension.h"

@implementation UIImage (Extension)
/**
*  返回一张自由拉伸的图片
*/
+ (UIImage *)resizedImageWithName:(NSString *)name{
    return [UIImage resizedImageWithName:name left:0.5 top:0.5];
}

+(UIImage *)resizedImageWithName:(NSString *)name left:(CGFloat)left top:(CGFloat)top{
    UIImage *image = [self imageNamed:name];
    return [image stretchableImageWithLeftCapWidth:image.size.width * left topCapHeight:image.size.height * top];
}

- (UIImage *)getCirlImage{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.size.width, self.size.width), NO, 0.0);
    
    CGFloat radius = self.size.width * 0.5;
    CGRect rect = CGRectMake(0, 0, self.size.width, self.size.width);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextAddArc(context, radius, radius, radius, 0, M_PI * 2, 0);
    [UIColor redColor];
    CGContextClip(context);
    CGContextFillPath(context);
    
    [self drawInRect:rect];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

/**
 *  切圆图
 *
 *  @param bg 图片
 *
 *  @return 新的图片
 */

+(instancetype)getCirlImage:(NSString *)imageName{
    UIImage *backImage=[UIImage imageNamed:imageName];
    CGFloat imageX=4;
    CGFloat imageY=4;
    CGFloat imageW=backImage.size.height+2*imageX;
    CGFloat imageH=backImage.size.height+2*imageY;
    CGSize imageSize=CGSizeMake(imageW, imageH);
    //创建一个Bitmap的ImageView对象(尺寸，是否透明，缩放比例)
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    //获取上下文
    CGContextRef context= UIGraphicsGetCurrentContext();
    CGFloat bigRadius=imageH*0.5;
    CGFloat bigCirlX=bigRadius;
    CGFloat bigCirlY=bigRadius;
    [[UIColor whiteColor]set];
    CGContextAddArc(context, bigCirlX, bigCirlY, bigRadius, 0, M_PI*2, 0);
    CGContextFillPath(context);
    
    //小圆
    CGFloat smallRadis=bigRadius-imageX;
    CGContextAddArc(context, bigCirlX, bigCirlY, smallRadis, 0, M_PI*2, 0);
    CGContextClip(context);
    
    [backImage drawInRect:CGRectMake(imageX, imageY, backImage.size.width, backImage.size.height)];
    
    //生成新的图片
    UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
    
    //结束上下文
    UIGraphicsEndImageContext();
    
    return newImage;

}

+ (UIImage *)imageWithColor:(UIColor *)color{
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

    // 开启位图上下文
    UIGraphicsBeginImageContext(rect.size);
    // 获取位图上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 渲染上下文
    CGContextFillRect(context, rect);
    // 从上下文中获取图片
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
//    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:4];
//    [path addClip];
//    [theImage drawInRect:rect];
    // 结束上下文
    UIGraphicsEndImageContext();

    return theImage;
}


/**
 *  生成高清图片
 *
 *  @param CIImage 图片
 *
 *  @return 新的图片
 */
+ (UIImage *)createHDImageFormCIImage:(CIImage *)image withSize:(CGFloat) size{
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}
@end
